package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.View;

import com.bumptech.glide.BitmapTypeRequest;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.ContactVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.databaseobject.StaffDo;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.RoundImageViewTarget;

import java.util.ArrayList;

/**
 * 最近联系人
 */
public class ContactAdapter extends BaseAdapter1<ContactVH> {

    private BitmapTypeRequest<String> mBitmapTypeRequest;
    private final ArrayList<StaffDo> list;
    private final RecyclerViewItemOnClickListener<StaffDo> itemOnClickListener;
    private boolean imEnable = true;

    public ContactAdapter(Context context, BitmapTypeRequest<String> mBitmapTypeRequest, ArrayList<StaffDo> list,
                          RecyclerViewItemOnClickListener<StaffDo> itemOnClickListener) {
        super(context);
        this.mBitmapTypeRequest = mBitmapTypeRequest;
        this.list = list;
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setImEnable(boolean imEnable) {
        this.imEnable = imEnable;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_contact;
    }

    @Override
    protected ContactVH viewHolder(View view) {
        return new ContactVH(view);
    }


    @Override
    public void onBindViewHolder(ContactVH holder, int position) {
        final StaffDo staffDo = list.get(position);
        if (GlideLoad.needLoad(true)) {
            mBitmapTypeRequest
                    .load(staffDo.getImgUrl())
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(holder.img_staff));
        } else {
            mBitmapTypeRequest
                    .load("")
                    .placeholder(R.drawable.ic_staff_default)
                    .error(R.drawable.ic_staff_default)
                    .centerCrop()
                    .into(new RoundImageViewTarget(holder.img_staff));
        }
        holder.atv_name.setText(staffDo.getCnName());
        holder.atv_im.setVisibility(imEnable ? View.VISIBLE : View.GONE);
        holder.atv_im.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.onItemOnClick(staffDo, 1);
            }
        });
        holder.atv_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.onItemOnClick(staffDo, 2);
            }
        });
        holder.atv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.onItemOnClick(staffDo, 3);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
